﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using Tuan.Model;
public partial class Pages_Admin_ProductDetail :NoTheme
{
    protected void Page_Load(object sender, EventArgs e)
    {
       
        if (Request["pid"] != null)
            dtvProducts.DefaultMode = DetailsViewMode.Edit;
    }
    protected void odsProducts_Inserting(object sender, ObjectDataSourceMethodEventArgs e)
    {
        HttpFileCollection files = Request.Files;
        string fileExtension = System.IO.Path.GetExtension(files[0].FileName.ToLower());
        string floderPath = string.Format("~/UploadFile/{1}_{0}", DateTime.Today.Year, DateTime.Today.Month);
        try
        {
            ((Products)e.InputParameters[0]).EditorBy = Page.User.Identity.Name;
            if (fileExtension.Length > 2)
            {
                if (CreatePath(floderPath))
                {
                    string relativePath = floderPath + "/" + Guid.NewGuid().ToString() + fileExtension;
                    string filePath = HttpContext.Current.Server.MapPath(string.Format("{0}", relativePath));
                    files[0].SaveAs(filePath);
                    ((Products)e.InputParameters[0]).ShowImage = relativePath;
                }
                else
                {

                    Pan.Common.MessageBox.Show(this, "创建文件路径失败!");
                }
            }
         
        }
        catch
        {
            Pan.Common.MessageBox.Show(this,"上传图片时，发生系统错误，请稍后再试!");
        }
    }

    #region

    /// <summary>
    ///panbbb CreatePath
    /// </summary>
    /// <param name="_path">Relateive Path</param>
    /// <returns>return Created state true or false</returns>
    protected static bool CreatePath(string _path)
    {
        bool yesOrno = true;
        try
        {
            string jia = HttpContext.Current.Server.MapPath(string.Format("{0}", _path));
            if (!Directory.Exists(jia))
            {
                Directory.CreateDirectory(jia);
                //   DirectoryInfo di = Directory.CreateDirectory(jia);
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return yesOrno;

    }
    #endregion

    protected void dtvProducts_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
    {
        if (e.Exception != null)
        {
            e.ExceptionHandled = true;
            e.KeepInInsertMode = true;
            Pan.Common.MessageBox.Show(this, "未能添加成功，请稍候再试");
            Pan.Log.WriteLog("\r\nAdmin_productDetails页面添加产品失败;Msg:" + e.Exception.Message, "Admins_Products");
        }
        else
        {
            Pan.Common.MessageBox.Show(this, "添加成功!");
        }
    }

    protected void dtvProducts_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
    {
        if (e.Exception != null)
        {
            e.ExceptionHandled = true;
            e.KeepInEditMode = true;
            Pan.Common.MessageBox.Show(this, "未能修改成功，请稍候再试");
            Pan.Log.WriteLog("\r\nAdmin_productDetails页面添加产品失败;Msg:" + e.Exception.Message, "Admins_Products");
        }
        else
        {
            Pan.Common.MessageBox.Show(this, "修改成功!");
        }
    }
}
